class Solution {
    public String frequencySort(String s) {
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        StringBuffer sb = new StringBuffer();
        List<Character> list = new ArrayList<Character>(map.keySet());
        Collections.sort(list, (a, b) -> map.get(b) - map.get(a));

        for (int i = 0; i < list.size(); i++) {
            char c = list.get(i);
            for (int j = 0; j < map.get(c); j++) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}